﻿

using QFramework;
using System.Linq;

namespace ProjectChildhood
{
    /// <summary>
    /// 增添物体的命令类(命令设计模式)
    /// </summary>
    public class AddItemCountCommand : AbstractCommand<bool>
    {

        private readonly int m_AddCount;
        private string m_ItemName;

        public AddItemCountCommand(string itemName, int addCount)
        {
            m_AddCount = addCount;
            m_ItemName = itemName;
        }

        protected override bool OnExecute()
        {

            if (m_ItemName.IsNullOrEmpty()) // 种子盲盒
            {
                int rand = UnityEngine.Random.Range(1, 6);
                if (rand == 1) m_ItemName = "seed_carrot";
                else if (rand == 2) m_ItemName = "seed_pumpkin";
                else if (rand == 3) m_ItemName = "seed_potato";
                else if (rand == 4) m_ItemName = "seed_tomato";
                else if (rand == 5) m_ItemName = "seed_bean";
            }
            var system = this.GetSystem<IToolBarSystem>();
            ToolbarSlot slot = system.Slots.FirstOrDefault(slot => slot.ItemName == m_ItemName); // 查看是否已经拥有

            if (slot == null)
            {
                slot = this.GetSystem<IToolBarSystem>().Slots.FirstOrDefault(slot => slot.Count == 0); // 找一个空位
                if (slot == null)
                {
                    UIMessageQueue.Push(null, "工具栏已满！");
                    return false;
                }
                slot.ItemName = m_ItemName;
                slot.Count.Value = m_AddCount;
            }
            else
            {
                slot.Count.Value += m_AddCount;
            }
            ToolBarSystem.OnItemCountChanged.Trigger(slot, slot.Count);
            UIMessageQueue.Push(ResController.Instance.LoadToolSprite(Config.ItemWithName[m_ItemName].Icon), $"已获得 {m_ItemName} * 1");
            return true;
        }
    }
}
